﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TotalFreedomAngularJS
{

    public interface IAjAttributeHtmlString<TModel> : IHtmlString
    {
        bool HasAjAttribute(string attributeName);
        bool IsAjAttributeSupported(string attributeName);
        List<KeyValuePair<string, string>> GetAttributeValues();
    }

    public class AjAttributeHtmlString<TModel, TProperty> : IHtmlString, IAjAttributeHtmlString<TModel>
    {
        internal readonly string _attributeValue;
        private readonly string _attributeName;
        private readonly string[] _supportedAttributes;

        public AjAttributeHtmlString(IAjAttributeHtmlString<TModel> parent, string attributeName, string attributeValue, object viewData = null, string[] supportedAttributes = null)
            : base()
        {
            if (String.IsNullOrWhiteSpace(attributeName))
                throw (new Exception("Attribute name must be non-empty"));

            _attributeValue = attributeValue ?? String.Empty;
            _attributeName = attributeName;
            _supportedAttributes = supportedAttributes;
        }

        public virtual bool HasAjAttribute(string attributeName)
        {
            if (_attributeName == attributeName)
                return true;

            return false;
        }

        public static bool IsNullOrEmpty(AjAttributeHtmlString<TModel, TProperty> value)
        {
            return (value == null || value._attributeValue.Length == 0);
        }

        public virtual string ToHtmlString()
        {
            string s = " " + _attributeName + "=\"" + _attributeValue + "\"";

            return s;
        }

        public virtual bool IsAjAttributeSupported(string attributeName)
        {
            if (_supportedAttributes != null && (!_supportedAttributes.Contains(attributeName)))
                return false;

            return true;
        }

        public List<KeyValuePair<string, string>> GetAttributeValues()
        {
            return new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>(_attributeName, _attributeValue) };
        }
    }

}
